package org.jiamandan.com.strategy.listener;


import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.strategy.IDXContextListener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service(value = DXConstant.Service.CMD_F01)
@AllArgsConstructor
@Slf4j
public class DXF01UpgradeUpgradeListener  implements IDXContextListener {

    private final ApplicationEventPublisher eventPublisher;

    @Override
    public void doExecute(DXBasicDto dto, Channel ctx) throws Exception {
        log.info("接收嘉满电-DX获取F01[升级握手指令]指令(CMD=F01)【"+dto.getDeviceNo()+"】报文:" + JSON.toJSONString(dto));
        //1.判断是否需要更新，
        //2.存储数据到redis
        //3.hash 记录更新点
        String data1 = dto.getData().substring(0, 8);
        String data2 = dto.getData().substring(8,16);
        StringBuilder version = new StringBuilder();
        for (int i = 0; i < data2.substring(0, 8).length()/2; i++) {
            version.append(StringUtils.leftPad(String.valueOf(Integer.valueOf(data2.substring(i * 2, (i + 1) * 2), 16)),2,"0"));
        }
        log.info("监听DX设备升级event,deviceNo={},version={}" ,dto.getDeviceNo(),version);

    }
}
